package com.demo.mq.rocket.provider.simple;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

import static com.demo.mq.rocket.provider.Const.ROCKET_DEFAULT_GROUP;
import static com.demo.mq.rocket.provider.Const.ROCKET_NAMESRVADDR;

/**
 * 同步,当前线程等待确认发送完成
 */
public class SyncProducer {

    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer producer = new DefaultMQProducer(ROCKET_DEFAULT_GROUP);
        producer.setNamesrvAddr(ROCKET_NAMESRVADDR);
        producer.start();
        for (int i = 0; i < 100; i++) {
            Message msg = new Message("simple_topic",
                    "TagS",
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
            );
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        }
        producer.shutdown();
    }
}
